package world.criver.injectmoduleinfo.function;

import world.criver.injectmoduleinfo.modular.moResult;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class funInject {
    private Runtime run;
    private File cache;
    private File target_parent=null;
    public funInject(){
        run=Runtime.getRuntime();
        iniCache();
    }
    /**
     * 生成模块信息java文件
     * @param jar_url jar路径
     * @return
     */
    public moResult create_modular_info(String jar_url){
        clearCache();
        moResult mo=new moResult();
        try {
            String c=readCmd(run.exec(STR."jdeps --ignore-missing-deps --generate-module-info \{cache.getAbsolutePath()} \{jar_url}"));
            mo.setState(true);
            mo.setContent(c);
        } catch (IOException e) {
            e.printStackTrace();
            mo.setState(false);
            mo.setContent(e.getMessage());
        }
        return mo;
    }
    /**
     * 编译 module-info.java
     * @param jar_url jar路径
     * @return
     */
    public moResult compile_module_info(String jar_url){
        File[] fs=cache.listFiles();
        for(File f:fs)if(f.isDirectory()){
            target_parent=f;
            break;
        }
        moResult mo=new moResult();
        mo.setState(false);
        if(target_parent==null){
            mo.setContent("No folder.");
            return mo;
        }
        File target=new File(target_parent.getAbsolutePath()+"/module-info.java");
        if(!target.exists() || target.isDirectory()){
            mo.setContent("No module-info.java file available.");
            return mo;
        }
        try {
            String c=readCmd(run.exec(STR."javac -p \{jar_url} --patch-module \{target_parent.getName()}=\{jar_url} \{target.getAbsolutePath()}"));
            mo.setState(c.isEmpty());
            mo.setContent(c);
        } catch (IOException e) {
            e.printStackTrace();
            mo.setContent(e.getMessage());
        }
        return mo;
    }

    /**
     * 注入 module-info.class
     * @param jar_url jar路径
     * @return
     */
    public moResult inject_module_info(String jar_url){
        moResult mo=new moResult();
        try {
            String c=readCmd(run.exec(STR."jar uf \{jar_url} -C \{target_parent.getAbsolutePath()} module-info.class"));
            mo.setState(c.isEmpty());
            mo.setContent(c);
        } catch (IOException e) {
            e.printStackTrace();
            mo.setState(false);
            mo.setContent(e.getMessage());
        }
        return mo;
    }

    /**
     * 资源管理器中打开文件夹
     */
    public void openFolder(){
        if(target_parent!=null) {
            try {
                run.exec(STR."explorer \{target_parent.getAbsolutePath()}");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    /**
     * 读取cmd执行的返回值
     * @param p 执行回调
     * @return
     */
    private String readCmd(Process p){
        BufferedReader re=new BufferedReader(new InputStreamReader(p.getInputStream()));
        String c="",r;
        try {
            while ((r = re.readLine()) != null) c += r;
            re.close();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return c;
    }
    /**
     * 初始化缓存文件夹
     */
    private void iniCache(){
        File f=new File("cache");
        if(!f.exists() || f.isFile())f.mkdirs();
        cache=f;
    }

    /**
     * 清空缓存
     */
    private void clearCache(){
        File[] fs=cache.listFiles();
        for(File f:fs)removeFile(f);
    }
    /**
     * 删除文件
     * @param f
     */
    private void removeFile(File f){
        if(f.isDirectory()){
            File[] fs=f.listFiles();
            for(File ff:fs)removeFile(ff);
        }
        f.delete();
    }
}
